package com.geekcattle.controller.goods;

import com.alibaba.fastjson.JSON;
import com.geekcattle.core.utils.ShiroUtil;
import com.geekcattle.service.goods.GoodsCarService;
import com.geekcattle.service.goods.GoodsOrderService;
import com.geekcattle.util.MapUtil;
import com.geekcattle.util.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @program: geek-framework
 * @description: 商品订单
 * @author: LQ
 * @create: 2019-04-21 13:59
 */
@RestController
@RequestMapping("/goodsOrder")
public class GoodsOrderController {
    @Autowired
    private GoodsOrderService goodsOrderService;
    @Autowired
    private GoodsCarService goodsCarService;

    /**
     * 添加订单
     * 一个商品(应该和添加多个商品合并为一个方法)
     *
     * @param map
     * @return
     */
    @RequestMapping(value = "/addOrder", method = {RequestMethod.GET, RequestMethod.POST, RequestMethod.PUT})
    public Map addOrder(@RequestParam Map map) {
        if (map == null) {
            return MapUtil.infoMap("订单添加失败,没有传递信息");
        }
        //订单id
        String uuid = UuidUtil.getUUID();
        map.put("order_id", uuid);
        map.put("one_order", uuid);
        //下单时间
        map.put("order_time", new Date());
        //订单主人
        map.put("created_by", this.getId());
        int result = goodsOrderService.addOrder(map);
        return MapUtil.infoMap(result, result > 0 ? "订单添加成功" : "订单添加失败");
    }

    /**
     * 添加订单
     * 订单中有多个商品
     *
     * @param map
     * @return
     */
    @RequestMapping(value = "/addOrders", method = {RequestMethod.GET, RequestMethod.POST, RequestMethod.PUT})
    public Map addOrders(@RequestParam Map map) {
        System.out.println("map:" + map);
        if (map == null || map.get("data") == null) {
            return MapUtil.infoMap("订单添加失败,没有传递信息");
        }
        List<Map> listMap = (List<Map>) JSON.parse(map.get("data") + "");
        String uuid = UuidUtil.getUUID();
        Date date = new Date();
        Iterator<Map> iterator = listMap.iterator();
        while (iterator.hasNext()) {
            Map goods = iterator.next();
            if(goods.get("order_price") == null) {
                String goodsCount = goods.get("goods_count") + "";
                String goodsPrice = goods.get("goods_price") + "";
                if (goodsCount == null || goodsPrice == null || "".equals(goodsCount) || "".equals(goodsPrice)) {
                    continue;
                }
                goods.put("order_price", Double.parseDouble(goodsCount) * Double.parseDouble(goodsPrice));
            }
            goods.put("created_by", this.getId());
            goods.put("order_time", date);
            goods.put("one_order", uuid);
            goods.put("order_id", UuidUtil.getUUID());
            try {
                goodsOrderService.addOrder(goods);
                if (goods.get("carId") != null) {
                    int result = goodsCarService.deleteCar(goods);
                    if (result <= 0) {
                        throw new Exception();
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                return MapUtil.infoMap("订单添加失败");
            }
        }
        return MapUtil.infoMap("订单添加成功");
    }

    @RequestMapping("/updateOrder")
    public Map updateOrder(@RequestParam Map map) {
        //修改人
        map.put("updated_by", UuidUtil.getUUID());
        //修改时间
        if (map.get("exit_cause") != null || "0".equals(map.get("order_status"))) {
            map.put("order_exit_time", new Date());
            map.put("order_status", 0);
        } else {
            map.put("updated_time", new Date());
        }
        int result = goodsOrderService.updateOrder(map);
        return MapUtil.infoMap(result, result > 0 ? "订单修改成功" : "订单修改失败");
    }

    /**
     * 订单查询
     *
     * @return
     */
    @RequestMapping(value = "/selectOrder", method = {RequestMethod.GET, RequestMethod.POST})
    public Map selectOrder(@RequestParam Map map) {
        map = MapUtil.limitMap(map);
        if (map.get("order_status") == null || "".equals(""+map.get("order_status"))){
            map.put("order_status",null);
        }
        String uid = this.getId();
        if (isUser(uid)) {
            map.put("created_by", uid);
        } else {
            map.put("created_by", null);
        }
        List<Map> result = goodsOrderService.selectOrder(map);
        int count = goodsOrderService.getOrderCount(map);
        return MapUtil.resultMap(count, result);
    }

    /**
     * 订单详情查询
     *
     * @return
     */
    @RequestMapping(value = "/selectOrderDetail", method = {RequestMethod.GET, RequestMethod.POST})
    public Map selectOrderDetail(@RequestParam Map map) {
        List<Map> result = goodsOrderService.selectOrderDetail(map);
        return MapUtil.resultMap(result);
    }

    /**
     * 获取当前登录用户的id
     *
     * @return
     */
    private String getId() {
        String uid = ShiroUtil.getUserInfo().getUid();
        return uid;
    }

    /**
     * 判断是不是用户登录的
     * 是返回true
     * 不是返回false
     *
     * @return
     */
    private boolean isUser(String uid) {
        String result = goodsOrderService.isUser(uid);
        if ("a570f6377cf34985b854d6ed04f50ca7".equals(result)) {
            return true;
        }
        return false;
    }

    /**
     * 是否是用户登录
     *
     * @param map
     * @return
     */
    @RequestMapping(value = "/isUser", method = {RequestMethod.GET, RequestMethod.POST})
    public Map isUsers(@RequestParam Map map) {
        if (this.isUser(this.getId())) {
            return MapUtil.resultMap("isUser");
        }
        return MapUtil.resultMap("noUser");
    }
}